package com.example.designpatterns.structural.proxy;

import java.util.Arrays;
import java.util.List;

/**
 * 代理
 *
 * @author scott
 */
public class ProxyInternet implements Internet {

    private Internet realInternet;

    public ProxyInternet() {
        realInternet = new RealInternet();
    }

    @Override
    public void connectTo(String serverHost) {
        // 添加访问控制逻辑
        if (isAccessAllowed(serverHost)) {
            realInternet.connectTo(serverHost);
        } else {
            System.out.println("Access to " + serverHost + " is denied.");
        }
    }

    private boolean isAccessAllowed(String serverHost) {
        // 实现访问控制的逻辑
        // 在此示例中，我们简单地假设只有白名单中的服务器可以访问
        List<String> allowedServers = Arrays.asList("server1.com", "server2.com", "server3.com");
        return allowedServers.contains(serverHost);
    }

}
